package com.ndp.fb.mq.service.messageservice;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.enums.message.ChannelType;
import com.ndp.fb.mq.api.producer.PushNotificationProducerService;
import com.ndp.fb.mq.api.service.messageservice.EventNotificationMessageService;
import com.ndp.fb.rdb.api.EventNotificationMessageRdbService;
import com.ndp.fb.rdb.api.UserRdbServices;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.rdb.model.message.EventNotificationMessage;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.util.MessageHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by elvisxiong on 7/24/15.
 */
@Service
public class EventNotificationMessageServiceImpl implements EventNotificationMessageService {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Reference
	EventNotificationMessageRdbService eventNotificationMessageRdbService;
	@Reference
	UserRdbServices userRdbServices;
	@Autowired
	@Qualifier("webSocketPushNotificationProducerService")
	PushNotificationProducerService webSocketPushNotificationProducerService;

	@Override
	public void sendMessage(EventNotificationMessage message, List<ChannelType> channelTypeList) {
		if (channelTypeList != null) {
			for (ChannelType channelType : channelTypeList) {
				switch (channelType) {
				case WEBSITE:
					message.setChannel(ChannelType.WEBSITE);
					Long id = eventNotificationMessageRdbService.save(message);
					message.setId(id);
					webSocketPushNotificationProducerService.sendPushNotifiction(message);
					break;
				case EMAIL:
					message.setChannel(ChannelType.EMAIL);
					id = eventNotificationMessageRdbService.save(message);
					message.setId(id);
					User user = userRdbServices.getUserByTicket(message.getReceiver());
					if (user != null) {
						if (!MessageHelper.sendMail(user.getEmail(), message.getSubject(), message.getBody())) {
							logger.error("send mail to {} fail", user.getEmail());
						}
					} else {
						logger.error("send mail get user {} fail", message.getReceiver());
						logger.error("message body :{}", message.getBody());
					}
					break;
				case SMS:
					message.setChannel(ChannelType.SMS);
					id = eventNotificationMessageRdbService.save(message);
					message.setId(id);
					user = userRdbServices.getUserByTicket(message.getReceiver());
					if (user != null) {
						if (!MessageHelper.sendMessage(user.getPhone(), "127.0.0.1", message.getBody())) {
							logger.error("send mail to {} fail", user.getEmail());
						}
					} else {
						logger.error("send mail get user {} fail", message.getReceiver());
						logger.error("message body :{}", message.getBody());
					}
					break;
				case IPHONE:
					message.setChannel(ChannelType.IPHONE);
					break;
				case ANDROID:
					message.setChannel(ChannelType.ANDROID);
					break;
				}
			}
		}
	}

	@Override
	public void sendMonitorMessage(EventNotificationMessage message, List<String> channelTypeList) {
		String receiver = message.getReceiver();
		List<String> receivers = CollectionUtils.arrayToList(receiver.split(","));
		if(CollectionUtils.isEmpty(receivers)){
			logger.error("send monitor message  fail receiver is null");
			return;
		}
		if (ListUtil.isNotEmpty(channelTypeList)) {
			for (int i=0;i<channelTypeList.size();i++) {
				String channelType = channelTypeList.get(i);
				switch (channelType) {
				case "WEBSITE":
					break;
				case "EMAIL":
					message.setChannel(ChannelType.EMAIL);

					if (receivers.contains("DEVOWNEREMAIL")) {
						String devs = ConfigCentre.getString("DEVOWNEREMAIL", "hunk.qin@ndpmedia.com");
						for (String dev : devs.split(",")) {
							MessageHelper.sendMail(dev, message.getSubject(), message.getBody());
						}
					}
					if (receivers.contains("PRODUCTOWNEREMAIL")) {
						String devs = ConfigCentre.getString("PRODUCTOWNEREMAIL", "lucky.zhan@ndpmedia.com");
						for (String dev : devs.split(",")) {
							MessageHelper.sendMail(dev, message.getSubject(), message.getBody());
						}
					}
					break;
				case "SMS":
					message.setChannel(ChannelType.SMS);
					if (receivers.contains("DEVOWNERPHONE")) {
						String devs = ConfigCentre.getString("DEVOWNERPHONE", "18611623083");
						for (String dev : devs.split(",")) {
							MessageHelper.sendMessage(dev, "127.0.0.1", message.getBody());
						}
					}
					if (receivers.contains("PRODUCTOWNERPHONE")) {
						String devs = ConfigCentre.getString("PRODUCTOWNERPHONE", "18161855496");
						for (String dev : devs.split(",")) {
							MessageHelper.sendMessage(dev, "127.0.0.1", message.getBody());
						}
					}
					break;
				case "IPHONE":
					message.setChannel(ChannelType.IPHONE);
					break;
				case "ANDROID":
					message.setChannel(ChannelType.ANDROID);
					break;
				}
			}
		}
	}
}
